package com.lmm.policy;

import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.TableRenderPolicy;
import com.deepoove.poi.util.TableTools;
import com.lmm.entity.BaseData;
import com.lmm.entity.DetailData;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.util.List;
import java.util.Objects;

/**
 * @Description 自定义动态表格处理
 * @Author lmm
 * @Date 2023/10/28 20:55
 */
public class DefaultSelfTableRenderPolicy extends DynamicTableRenderPolicy {
    // 货品填充数据所在行数
    final int requestRow = 5;
    // 人工费填充数据所在行数
    final int responseRow = 8;
    // table cell 长度
    final int cellLength = 4;
    @Override
    public void render(XWPFTable table, Object data) throws Exception {
        if (null == data) return;
        DetailData detailData = (DetailData) data;
        // 处理request
        List<RowRenderData> labors = detailData.getRequest();
        dealTable(table, labors, requestRow);
        // 处理response
        List<RowRenderData> goods = detailData.getResponse();
        dealTable(table, goods, responseRow + (Objects.nonNull(labors) ? (labors.size() - 1) : -1));
    }

    private void dealTable(XWPFTable table, List<RowRenderData> goods, int responseRow) throws Exception {
        table.removeRow(responseRow);
        if (null != goods) {
            for (RowRenderData good : goods) {
                XWPFTableRow insertNewTableRow = table.insertNewTableRow(responseRow);
                for (int j = 0; j < cellLength; j++) insertNewTableRow.createCell();
                TableRenderPolicy.Helper.renderRow(table.getRow(responseRow), good);
            }
        }
    }
}
